package com.engine.hitech.duss.lefttree.cmd;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.User;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Param:
 * @return:
 * @Author: Dusongsong
 * @Date: 2021/3/1
 */

public class LeftTreeCmd extends AbstractCommonCommand<Map<String, Object>> {

    public LeftTreeCmd(User user, Map<String, Object> params) {
        this.user = user;
        this.params = params;
    }

    @Override
    public BizLogContext getLogContext() {
        return null;
    }

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        if (null == user) {
            apidatas.put("hasRight", false);
            return apidatas;
        }
        apidatas.put("hasRight", true);
        try {
            RecordSet rs = new RecordSet();
            JSONArray datasArray = new JSONArray();
            String subcompanyid = "13";
            rs.executeQuery("select subcompanyname from hrmsubcompany where id=?", subcompanyid);
            if (rs.next()) {
                //分部名称
                String subcompanyname = Util.null2String(rs.getString("subcompanyname"));
                JSONObject datasJson = new JSONObject();
                datasJson.put("domid", "0_" + subcompanyid);
                datasJson.put("haschild", true);
                datasJson.put("isopen", true);
                datasJson.put("key", "type_0_" + subcompanyid);
                datasJson.put("name", subcompanyname);
                JSONArray childsArray = getSubCompany();
                datasJson.put("childs", childsArray);
                datasArray.add(datasJson);
            }
            apidatas.put("flag", true);
            apidatas.put("datas", datasArray.toJSONString());
        } catch (Exception e) {
            apidatas.put("flag", false);
            e.printStackTrace();
        }
        return apidatas;
    }

    //获取一级分部
    public static JSONArray getSubCompany() {
        JSONArray childsArray = new JSONArray();
        RecordSet rs = new RecordSet();
        rs.executeQuery(" select GSBER from uf_order where isnull(GSBER,0)>0  group by GSBER order by GSBER");
        while (rs.next()) {
            int subcompanyid = Util.getIntValue(rs.getString("GSBER"));
            String subCompanyname = 1000 == subcompanyid ? "封测PKG/T" : "模组Module";
            JSONObject childsJson = new JSONObject();
            childsJson.put("domid", "1_" + subcompanyid);
            childsJson.put("haschild", true);
            childsJson.put("isopen", true);
            childsJson.put("key", "type_1_" + subcompanyid);
            childsJson.put("name", subCompanyname);
            JSONArray subChildsArray = getSecDepartment(subcompanyid);
            childsJson.put("childs", subChildsArray);
            childsArray.add(childsJson);
        }
        return childsArray;
    }


    public static List<Map<String, Object>> getDepartment(int subcompanyid) {
        RecordSet rs = new RecordSet();
        List<Map<String, Object>> mapList = new ArrayList();
        rs.executeQuery("select * from view_custom_depart where subcompanyid=?", subcompanyid);
        while (rs.next()) {
            Map<String, Object> dataMap = new HashMap<>();
            dataMap.put("id", Util.null2String(rs.getString("id")));
            dataMap.put("supdepid", Util.null2String(rs.getString("supdepid")));
            dataMap.put("tlevel", Util.null2String(rs.getString("tlevel")));
            dataMap.put("subcompanyid", Util.null2String(rs.getString("subcompanyid")));
            dataMap.put("departmentcode", Util.null2String(rs.getString("departmentcode")));
            dataMap.put("departmentname", Util.null2String(rs.getString("departmentname")));
            mapList.add(dataMap);
        }
        return mapList;
    }

    //获取二级部门
    public static JSONArray getSecDepartment(int subcompanyid) {
        JSONArray childsArray = new JSONArray();
        List<String> firstDepartList = new ArrayList<>();
        List<Map<String, Object>> mapList = getDepartment(subcompanyid);
        for (Map<String, Object> map : mapList) {
            String supdepid = (String) map.get("supdepid");
            if (!firstDepartList.contains(supdepid)) {
                firstDepartList.add(supdepid);
            }
        }
        for (String supdepid : firstDepartList) {
            String departmentcode = getCodeAndName(supdepid, "2").get("departmentcode");
            String departmentname = getCodeAndName(supdepid, "2").get("departmentname");
            JSONObject childsJson = new JSONObject();
            childsJson.put("domid", "2_" + subcompanyid + "_" + departmentcode);
            childsJson.put("haschild", false);
            childsJson.put("isopen", true);
            childsJson.put("key", "type_2_" + subcompanyid + "_" + departmentcode);
            childsJson.put("name", departmentname);
            JSONArray subChildsArray = getThirdDepartment(mapList, supdepid);
            childsJson.put("childs", subChildsArray);
            childsArray.add(childsJson);
        }

        return childsArray;
    }

    //获取三级部门
    public static JSONArray getThirdDepartment(List<Map<String, Object>> mapList, String supdepidParam) {
        JSONArray childsArray = new JSONArray();
        for (Map<String, Object> map : mapList) {
            String supdepid = (String) map.get("supdepid");
            String tlevel = (String) map.get("tlevel");
            String subcompanyid = (String) map.get("subcompanyid");
            if (supdepidParam.equals(supdepid)) {
                String departid = (String) map.get("id");
                if ("4".equals(tlevel)) {
                    JSONObject childsJson = new JSONObject();
                    String departmentcode = getCodeAndName(supdepid, "3").get("departmentcode");
                    String departmentname = getCodeAndName(supdepid, "3").get("departmentname");
                    childsJson.put("domid", "3_" + subcompanyid + "_" + departmentcode);
                    childsJson.put("haschild", false);
                    childsJson.put("isopen", true);
                    childsJson.put("key", "type_3_" + subcompanyid + "_" + departmentcode);
                    childsJson.put("name", departmentname);
                    JSONArray subChildsArray = getFourDepartment(mapList,departid);
                    childsJson.put("childs", subChildsArray);
                    childsArray.add(childsJson);
                }else {
                    JSONObject childsJson = new JSONObject();
                    String departmentcode = (String) map.get("departmentcode");
                    String departmentname = (String) map.get("departmentname");
                    childsJson.put("domid", "3_" + subcompanyid + "_" + departmentcode);
                    childsJson.put("haschild", false);
                    childsJson.put("isopen", true);
                    childsJson.put("key", "type_3_" + subcompanyid + "_" + departmentcode);
                    childsJson.put("name", departmentname);
                    JSONArray subChildsArray =  getFiveDepartment(departid,subcompanyid);
                    childsJson.put("childs", subChildsArray);
                    childsArray.add(childsJson);
                }

            }
        }
        return childsArray;
    }

    //获取四级部门
    public static JSONArray getFourDepartment(List<Map<String, Object>> mapList,String departidParam) {
        JSONArray childsArray = new JSONArray();
        for (Map<String, Object> map : mapList) {
            String subcompanyid = (String) map.get("subcompanyid");
            String departid = (String) map.get("id");
            if (departidParam.equals(departid)) {
                JSONObject childsJson = new JSONObject();
                    String departmentcode = (String) map.get("departmentcode");
                    String departmentname = (String) map.get("departmentname");
                    childsJson.put("domid", "4_" + subcompanyid + "_" + departmentcode);
                    childsJson.put("haschild", false);
                    childsJson.put("isopen", true);
                    childsJson.put("key", "type_4_" + subcompanyid + "_" + departmentcode);
                    childsJson.put("name", departmentname);
                    JSONArray subChildsArray = getFiveDepartment(departid, subcompanyid);
                    childsJson.put("childs", subChildsArray);

                childsArray.add(childsJson);
            }
        }
        return childsArray;
    }
    //获取五级部门
    public static JSONArray getFiveDepartment(String supdepidParam, String subcompanyid) {
        JSONArray childsArray = new JSONArray();
        RecordSet rs = new RecordSet();
        String sql = "select departmentcode,departmentname from HrmDepartment where isnull(departmentcode,'') !='' and supdepid=?";
        rs.executeQuery(sql, supdepidParam);
        while (rs.next()) {
            String departmentcode = Util.null2String(rs.getString("departmentcode"));
            String departmentname = Util.null2String(rs.getString("departmentname"));
            JSONObject childsJson = new JSONObject();
            childsJson.put("domid", "5_" + subcompanyid + "_" + departmentcode);
            childsJson.put("haschild", false);
            childsJson.put("isopen", true);
            childsJson.put("key", "type_5_" + subcompanyid + "_" + departmentcode);
            childsJson.put("name", departmentname);
            childsArray.add(childsJson);
        }
        return childsArray;
    }
    public static Map<String, String> getCodeAndName(String id, String tlevelParam) {
        RecordSet rs = new RecordSet();
        Map<String, String> departMap = new HashMap<>();
        rs.executeQuery(" select departmentcode,departmentname,tlevel from HrmDepartment where id=?", id);
        rs.next();
        String tlevel = Util.null2String(rs.getString("tlevel"));
        if (tlevelParam.equals(tlevel)) {
            departMap.put("departmentcode", Util.null2String(rs.getString("departmentcode")));
            departMap.put("departmentname", Util.null2String(rs.getString("departmentname")));
        } else {
            rs.executeQuery("select b.departmentcode,b.departmentname\n" +
                    "  from HrmDepartment a join HrmDepartment b on a.supdepid=b.id where a.id=?", id);
            rs.next();
            departMap.put("departmentcode", Util.null2String(rs.getString("departmentcode")));
            departMap.put("departmentname", Util.null2String(rs.getString("departmentname")));
        }

        return departMap;
    }


}
